package com.ivr.cm.http;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

public class IplimitFilter implements Filter {
	private static Logger logger = Logger.getLogger(IplimitFilter.class);
	
	private String[] ipLimits;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		String sourceIp = request.getRemoteAddr();
		logger.info(String.format("调用IP地址[%s]", sourceIp));
		for(String ip : ipLimits){
			if (sourceIp.startsWith(ip) || "*".equals(ip)){
				chain.doFilter(request, response);
				break;
			}
		}
	}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}
	@Override
	public void destroy() {
	}
	public void setIpLimit(String ipLimit) {
		this.ipLimits = ipLimit.trim().split(";");
	}
}